<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of candidate_requirements_controller
 *
 * @author wilson
 */
class PropertyRequirementscontroller extends AppController {

    //put your code here
    var $name = "PropertyRequirements";

    function index($evaluation_id) {

        $this->layout = "ajax";
        $this->PropertyRequirement->Requirement->recursive = -1;
        $call_id = 1;
        $requerimientos = $this->PropertyRequirement->Requirement->find(
                'all', array(
            'conditions' => array('Requirement.tipo' => 'Predio', 'Requirement.call_id' => $call_id),
            'fields' => array('Requirement.id'),
            'recursive' => -1,
                ));
       
        foreach ($requerimientos as $req) {


            $r = $req['Requirement']['id'];


            $cont = $this->PropertyRequirement->find('count', array('recursive' => -1, 'conditions' => array('PropertyRequirement.requirement_id' => $req['Requirement']['id'], 'PropertyRequirement.property_evaluation_id' => $evaluation_id)));
            if ($cont == 0) {

                $this->PropertyRequirement->query("INSERT INTO property_requirements (property_evaluation_id,requirement_id)values($evaluation_id,$r)");
            }
        }
        $requerimientos = $this->PropertyRequirement->find('all',array('recursive'=>0, 'conditions'=>array('PropertyRequirement.property_evaluation_id'=>$evaluation_id),'fields'=>array('PropertyRequirement.id' ,'Requirement.texto', 'PropertyRequirement.calificacion')));
        $this->set('requisitos', $requerimientos);
    }

    function edit($id) {
      $this->layout="ajax";
        if (empty($this->data)) {
           
            $this->data = $this->PropertyRequirement->find('first', array('conditions' => array('PropertyRequirement.id' => $id), 'fields' => array('PropertyRequirement.id', 'PropertyRequirement.requirement_id', 'PropertyRequirement.property_evaluation_id', 'PropertyRequirement.concepto', 'PropertyRequirement.calificacion',  'Requirement.texto')));
        } else {
            
            
            if ($this->PropertyRequirement->saveAll($this->data)) {
               
                $this->redirect(array('controller'=>'PropertyRequirements','action'=>'index',$this->data['PropertyRequirement']['property_evaluation_id']));
                   
                $this->Session->setFlash('Registro guardado exitosamente');

            }
        }
    }

    
    
    

}

?>
